package org.flower.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.session.SqlSession;
import org.flower.bean.CommodityClass;
import org.flower.bean.CommodityClassExample;
import org.flower.mapper.CommodityClassMapper;
import org.flower.service.CommodityClassService;
import org.flower.utils.MyBatisUtil;

import java.io.IOException;
import java.util.List;


/**
 * @author Aizen
 */
public class CommodityClassServiceImpl implements CommodityClassService {

    @Override
    public PageInfo<CommodityClass> findAll(CommodityClass commodityClass,int startRow) throws IOException {
        SqlSession sqlSession = MyBatisUtil.getSqlSessionFactory();

        CommodityClassMapper classMapper = sqlSession.getMapper(CommodityClassMapper.class);

        CommodityClassExample classExample = new CommodityClassExample();

        CommodityClassExample.Criteria criteria = classExample.createCriteria();

        if(commodityClass.getName()!=null&&!commodityClass.getName().equals("")){
            criteria.andNameLike("%"+commodityClass.getName()+"%");
        }
        if(commodityClass.getType()!=null&&!commodityClass.getType().equals("")&&!commodityClass.getType().equals(0)){
            criteria.andTypeEqualTo(commodityClass.getType());
        }
        if(commodityClass.getPid()!=null&&!commodityClass.getPid().equals("")){
            criteria.andPidEqualTo(commodityClass.getPid());
        }


        PageHelper.startPage(startRow, 10);
        List<CommodityClass> commodityClasses = classMapper.selectByExample(classExample);
        PageInfo<CommodityClass> listPageInfo = new PageInfo<CommodityClass>(commodityClasses,10);
        sqlSession.close();
        return listPageInfo;
    }

    @Override
    public List<Integer> findAllType() throws IOException {
        SqlSession sqlSession = MyBatisUtil.getSqlSessionFactory();
        CommodityClassMapper mapper = sqlSession.getMapper(CommodityClassMapper.class);
        List<Integer> allType = mapper.findAllType();
        sqlSession.close();
        return allType;
    }

    @Override
    public List<CommodityClass> findAll() {
        try {
            SqlSession sqlSessionFactory = MyBatisUtil.getSqlSessionFactory();
            CommodityClassMapper mapper = sqlSessionFactory.getMapper(CommodityClassMapper.class);
            List<CommodityClass> classList = mapper.selectByExample(null);
            sqlSessionFactory.close();
            return classList;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<CommodityClass> findClassByPid(int pid) {
        SqlSession sqlSessionFactory = null;
        try {
            sqlSessionFactory = MyBatisUtil.getSqlSessionFactory();
        } catch (IOException e) {
            e.printStackTrace();
        }
        CommodityClassMapper mapper = sqlSessionFactory.getMapper(CommodityClassMapper.class);
        CommodityClassExample classExample = new CommodityClassExample();
        CommodityClassExample.Criteria criteria = classExample.createCriteria();
        criteria.andPidEqualTo(pid);
        List<CommodityClass> classList = mapper.selectByExample(classExample);
        sqlSessionFactory.close();
        return classList;
    }

}
